define(['app', 'js/services/MessageBox', 'js/services/MuseService',
    'js/directives/DatetimePicker', 'js/directives/FileUpload'], function (app) {

    var deps = ['$scope', '$state', '$modal', '$stateParams', 'MessageBox', 'MuseService', '$modalInstance', 'selfObj'];

    function controller ($scope, $state, $modal, $stateParams, MessageBox, MuseService, $modalInstance, selfObj) {
        //获取参数
        //var sysCorpusTypeId = $stateParams.sysCorpusTypeId || '';
        //$scope.sysCorpusId = $stateParams.sysCorpusId || '';

        $scope.sysCorpusId = selfObj.sysCorpusId || '';
        var sysCorpusTypeId = selfObj.sysCorpusTypeId || '';


        $scope.sysCorpus = {};

        $scope.edit = function () {
            MuseService.post('admin/sysCorpus/get.json', {id: $scope.sysCorpusId}).success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        $scope.sysCorpus = data.data;

                    } else {
                        MessageBox.notify('获取数据失败:' + data.message);
                    }
                });
        };
        if ($scope.sysCorpusId != '') {
            $scope.edit();
        }


        $scope.close = function () {
            $modalInstance.dismiss("cancel");
        };

        //保存
        $scope.save = function () {
            //TODO 1.保存前检查校验(非空）2.unique checkName.json
            $scope.sysCorpus.corpusTypeId = sysCorpusTypeId;
            MuseService.postObj('admin/sysCorpus/save.json', $scope.sysCorpus).success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        $scope.result = data.data;
                        MessageBox.notify({msg: '保存成功!', displayTime: 1000}).result.then(function () {
                            //$modalInstance.close("save");
                        }, function () {
                            //$modalInstance.close("save");
                        });
                        $modalInstance.close("save");
                        //history.back();

                    } else {
                        MessageBox.error('保存失败：' + data.message);
                    }
                });
        };

    }

    controller.$inject = deps;
    app.lazy.controller('SysCorpusEditController', controller);
});
